//
//  NewFeatureViewController.swift
//  Weibo8023
//
//  Created by styshy on 15/12/10.
//  Copyright © 2015年 sz. All rights reserved.
//

import UIKit
import SnapKit

private let WBNewFeatureViewCellId = "WBNewFeatureViewCellId"
private let WBNewFeatureImageCount = 4

class NewFeatureViewController: UICollectionViewController {


    /// collectionView的初始化
    ///
    /// - returns: UICollectionView
    init(){
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        // 创建CollectionView
        super.init(collectionViewLayout: layout)
        
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: WBNewFeatureViewCellId)
        
        
    }

    
    
}

// MARK:- 代理方法设置
extension NewFeatureViewController{
    // cell的个数
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WBNewFeatureImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNewFeatureViewCellId, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
        return cell
        
    }
    
/// 监听scrollView的滚动
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page:Int = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != WBNewFeatureImageCount - 1 {
            return
        }
        
       let cell =  collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        
        cell.showButtonAnim()
    }
}


private class NewFeatureCell:UICollectionViewCell{
    private var imageIndex:Int = 0{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startBtn.hidden = true
        }
    }
    
    // 给开始体验按钮添加点击事件
    @objc private func clickStartButton(){
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
    
    // 显示按钮动画
    private func showButtonAnim(){
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.6,     // 动画时长
            delay: 0,                       // 延时时间
            usingSpringWithDamping: 0.6,    // 弹力系数，0~1，越小越弹
            initialSpringVelocity: 10,      // 初始速度，模拟重力加速度
            options: [],                    // 动画选项
            animations: { () -> Void in
                
                self.startBtn.transform = CGAffineTransformIdentity
                
            }) { (_) -> Void in
                self.startBtn.userInteractionEnabled = true
        }
        
    }
    
    // 初始化cell
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(iconView)
        addSubview(startBtn)
        // 设置按钮为隐藏
        startBtn.hidden = true
        
        iconView.frame = bounds
        
        // 设置按钮的约束条件
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        
        // 给按钮添加点击事件
        startBtn.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
        
    }
    
    // 将图片和按钮设置为懒加载
    private lazy var iconView:UIImageView  = UIImageView()
    private lazy var startBtn:UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), backImageName: "new_feature_finish_button")
}
